package com.fingard.dsp.bank.directbank.nnbill02;

import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IH5Order;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

import java.util.*;

public class NNBill02H5Order extends NNBill02Base implements IH5Order {
    @Override
    public RetAtsUnifiedOrder submitH5Order(ReqUnifiedOrder p_req) {
        RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
        retAts.rdSeq = p_req.rdSeq;
        try{
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "order");
            if (actItem == null) {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
                return retAts;
            }
            Map<String, Object> reqmap = new LinkedHashMap<String, Object>();
            reqmap.put("requestTime", Format.DateTime14Format.format(new Date()));
            reqmap.put("externalTraceNo",p_req.rdSeq);
            reqmap.put("merchantCode",actItem.inMerchantId);
            reqmap.put("merchantId",actItem.merchantId);
            reqmap.put("terminalId",actItem.termId);
            reqmap.put("branchId","");
            reqmap.put("branchName","");
            reqmap.put("amt",p_req.amount);
            reqmap.put("merchReqReserved",p_req.attach);
            reqmap.put("productInfo",p_req.description);

            if(StringHelper.hasAnyChar(p_req.alipayUid)&&StringHelper.hasAnyChar(p_req.paymentNo) || "0".equals(p_req.realNameType)){
                reqmap.put("needAuth","1");
                JSONObject authInfo = new JSONObject();
                authInfo.put("authMode","01");
                authInfo.put("authPayId",p_req.alipayUid);
                authInfo.put("authOrderId",p_req.paymentNo);
                reqmap.put("authInfo",authInfo);
            }
            reqmap.put("returnUrl",getBankConfig().getValueAsString("returnUrl")+"?rdSeq="+p_req.rdSeq);
            //reqmap.put("txnExpireTime",p_req.timeExpire);
            String digest = sign(reqmap,actItem.saltKey);
            WriteBankLogLn("生成签名串："+digest);
            reqmap.put("secretInfo",digest);
            String url = convertMapToStr(reqmap);
            HashMap<String, String> tmpRetMap = new HashMap<String, String>();
            tmpRetMap.put("Result", bankFront.getUrl("pre")+"?"+url);

            retAts.respCode = "0";
            retAts.respInfo = "签名成功";
            retAts.customHashMap = tmpRetMap;
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }

    @Override
    public RetAtsQueryOrder queryH5Pay(ReqQueryOrder p_req) {
        RetAtsQueryOrder retAts = new RetAtsQueryOrder();
        retAts.rdSeq = p_req.rdSeq;
        try{
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "order");
            if (actItem == null) {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
                return retAts;
            }
            Map<String, Object> reqmap = new HashMap<String, Object>();
            reqmap.put("memberCode",actItem.inMerchantId);
            reqmap.put("merchantId",actItem.merchantId);
            reqmap.put("merchantName",actItem.userCode);
            reqmap.put("terminalId",actItem.termId);
            reqmap.put("requestTime", Format.DateTime14Format.format(new Date()));
            reqmap.put("externalTraceNo", p_req.rdSeq);
            reqmap.put("origTxnType", "PUR");
            String digest = sign(reqmap,actItem.saltKey);
            WriteBankLogLn("生成签名串："+digest);
            reqmap.put("secretInfo",digest);
            WriteBankUrlLogLn(bankFront.serverURL+"/query");
            String[] tmpStrRet = sendToBank(reqmap,bankFront.getUrl("")+"/query");
            if(tmpStrRet[0].length() == 0){
                JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                retAts.respCode = "0";
                retAts.respInfo = resultObj.get("responseMessage").toString();

                String transState = resultObj.get("txnFlg").toString();
                if (StringHelper.hasAnyChar(transState)) {
                    retAts.transState = transState;
                    retAts.payInfoCode = resultObj.get("responseCode").toString();
                    retAts.outTransID = resultObj.get("externalTraceNo").toString();
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = resultObj.get("responseMessage").toString();
                }
            }else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
